from openpyxl import Workbook
from openpyxl.chart import (
    Reference,
    Series,
    BarChart3D,
)

# 创建工作表
wb = Workbook()
ws = wb.active

# 创建数据
rows = [
    (None, 2013, 2014),
    ("Apples", 5, 4),
    ("Oranges", 6, 2),
    ("Pears", 8, 3)
]

# 追加数据
for row in rows:
    ws.append(row)

# 提取第2列
data = Reference(ws, min_col=2, min_row=1, max_col=3, max_row=4)
# 提取第1列
titles = Reference(ws, min_col=1, min_row=2, max_row=4)
# 创建3D条形图
chart = BarChart3D()
chart.title = "3D 条形图"
chart.add_data(data=data, titles_from_data=True)
chart.set_categories(titles)

ws.add_chart(chart, "E5")
wb.save("bar5.xlsx")
